package com.taptech.doufu.db.history;

import com.taptech.doufu.util.TTLog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper
{

    public static final String DBNAME = "readRecord.db";
    public static final String TABLE_NAME = "readinfo";
    public static final String TABLE_ANALYSIS_NAME = "analysis";

    public DatabaseHelper(Context context, String name, CursorFactory factory, int version)
    {
        super(context, name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        TTLog.i("TAG1", "DatabaseHelper onCreate");
        Cursor cursor = null;
        try
        {
            cursor = db.rawQuery("SELECT * FROM sqlite_master WHERE type = ? AND name = ?", new String[]{"table", TABLE_NAME});
            if (cursor.moveToNext())
            {
            } else
            {
                db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
                        "_ID INTEGER PRIMARY KEY autoincrement, " + "novelid VARCHAR(1024), " + "sectionid VARCHAR(1024), " + "position INTEGER, "
                        + "allnum INTEGER, " + "name VARCHAR(1024), " + "time VARCHAR(1024), "+ "imgurl VARCHAR(1024),"    + "progress VARCHAR(1024), " + "type INTEGER, "+"needSync INTEGER " + ");");

            }
            cursor.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        TTLog.i("TAG1", "onUpgrade");
//        db.execSQL("drop table if exists " + TABLE_NAME);
        db.execSQL("drop table if exists " + TABLE_ANALYSIS_NAME);
//        onCreate(db);
        if (!checkColumnExists(db,TABLE_NAME,"progress")){
            String sql1 = "ALTER TABLE "+TABLE_NAME+" ADD COLUMN progress VARCHAR";
            db.execSQL(sql1);
        }
        if (!checkColumnExists(db,TABLE_NAME,"type")){
            String sql2 = "ALTER TABLE "+TABLE_NAME+" ADD COLUMN type VARCHAR";
            db.execSQL(sql2);
        }
        if (!checkColumnExists(db, TABLE_NAME, "needSync")) {
            String sql3 = "ALTER TABLE "+TABLE_NAME+" ADD COLUMN needSync INTEGER";
            db.execSQL(sql3);
        }
    }


    /**
     * 方法：检查表中某列是否存在
     * @param db
     * @param tableName 表名
     * @param columnName 列名
     * @return
     */
    private boolean checkColumnExists(SQLiteDatabase db, String tableName, String columnName) {

        boolean result = false ;
        Cursor cursor = null ;

        try{
            cursor = db.rawQuery( "select * from sqlite_master where name = ? and sql like ?"
                    , new String[]{tableName , "%" + columnName + "%"} );
            result = null != cursor && cursor.moveToFirst() ;
        }catch (Exception e){
            Log.e(tableName,"checkColumnExists..." + e.getMessage()) ;
        }finally{
            if(null != cursor && !cursor.isClosed()){
                cursor.close() ;
            }
        }
        System.out.println("方法：检查表中某列是否存在"+columnName+"::"+result);
        return result ;
    }

}
